﻿@model MvcCmsWikiPlex.Models.ViewData.WikiModel
@{
    ViewBag.Title = Model._Portal.DisplayName + " Wiki - " + Model._WikiContent.WikiTitle.Name;
    Layout = "~/RazorThemes/Default/Views/Shared/_Layout.cshtml";
}
@section HtmlHead{
@if (!Model._WikiContent.IsCurrentVersion)
{
    <text><meta name="ROBOTS" content="NOINDEX, FOLLOW"></text>
}
<link href="@Url.Content("/RazorThemes/" + Model._Portal.SkinName + "/css/Wiki.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("/Content/start/jquery-ui-1.7.2.custom.css")" rel="stylesheet" type="text/css" />
}
<div>
<div id="wikiContent">
    <div id="originalWikiContent">
        @MvcHtmlString.Create(Model._WikiContent.RenderedSource)</div>
    <div id="previewWikiContent" style="display: none;">
    </div>
</div>
<div id="editWikiForm">
    @using (Html.BeginRouteForm("WikiAct", new { action = "EditWiki", Model._WikiContent.WikiTitle.Slug }, FormMethod.Post))
    {
        @Html.Hidden("Name", Model._WikiContent.WikiTitle.Name);
        <fieldset>
            <label for="Source">
                Source:</label>
            @if (Model._WikiContent.Version != Model._WikiContent.WikiTitle.MaxVersion)
            {
                <span id="editWikiNotLatest">Note: Not editing latest source </span>
            }
            @Html.TextArea("Source", string.Empty)
            <input type="submit" value="Save" />
            <input id="cancelEdit" type="button" value="Cancel" />
        </fieldset>
    }
</div>
</div>
@section SideBar{
@Html.Partial("ControlsWiki/Sidebar", Model)
}
@section ScriptContent{
<script src="@Url.Content("/Scripts/jquery-ui-1.7.2.custom.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    function SetPaging() {
        var pgnotmp = pageno;
        var startval = 0, endval = 0;
        if (pgnotmp > 1) {
            pgnotmp = (pageno * 10);
            startval = pgnotmp - 9;
            endval = pgnotmp;
        }
        else {
            startval = pgnotmp;
            endval = pgnotmp + 9;
        }
        $('#from').html(startval);
        if (endval > totalrecs) {
            $('#to').html(totalrecs);
        }
        else {
            $('#to').html(endval);
        }

        $('#total').html(totalrecs);
    }
    var totalrecs = @ViewData["HistoryCount"].ToString();
    var pageno = 0;
    GetHistory(1);

    function GetHistory(page) {
        var now = new Date();
        var pgnotmmp = pageno;
        pageno += page;
        var slug = '@Model._WikiContent.WikiTitle.Slug';
        if ((((pageno * 10) - 9) <= totalrecs) && pageno > 0) {
            $.post(
        "/wikiajax/GetHistory/" + pageno + "/" + slug,
        function (data) {
//            var objdata = JSON.parse(data);
            var objdata = data;
            if (objdata.error) {
                alert(objdata.error);
            }
            else {
                PopulateHistory(objdata, slug);
                SetPaging();
            }
        },
        "json");
        }
        else {
            pageno = pgnotmmp;
        }
    }

    function PopulateHistory(data, slug) {
        $("#wikiHistoryPaginated tbody").html("");
        var objdata = data;
        var tblRow = "<tr><th>Page History</th></tr>";
        $(tblRow).appendTo("#users-table tbody");
        $.each(objdata, function (i, history) {

            tblRow += "<tr>";

            if (@Model._WikiContent.Version == history.Version) {
                tblRow += "<td>" + myformater(history.VersionDate) + " - " + history.UserName + "</td>";
            }
            else {
                tblRow += "<td><a href=/wiki/" + slug;
                if (!history.IsCurrentVersion) {
                    tblRow += "/v" + history.Version;
                }
                tblRow += ">";
                tblRow += myformater(history.VersionDate) + "</a> - " + history.UserName;
                tblRow += "</td>";
            }

            tblRow += "</tr>";
            $(tblRow).appendTo("#wikiHistoryPaginated tbody");
            tblRow = "";
        });
    }
    function myformater(jsonDt) {
        var date = eval(jsonDt.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
        return date.format("mm/dd/yy h:MM tt");
    }

        var timeout = null;
        $(function () {
            var dlg = $('#editWikiForm');
            var cnt = $('#editWikiContent');
            var sidebar = $('#wikiSidebar');            
            var cntPos = sidebar.position();
            dlg.dialog({ autoOpen: false,
                width: 750,
                position: [cntPos.left + 150 + cnt.outerWidth(), cntPos.top + cnt.outerHeight()-50],
                resizable: true,
                beforeclose: function () { $('#originalWikiContent').show(); $('#previewWikiContent').hide(); }
            });
            cnt.click(function () {
                if (!dlg.dialog('isOpen')) {
                    $.post('@Url.RouteUrl("WikiSource", new { Model._WikiContent.WikiTitle.Slug, Model._WikiContent.Version }) ', function (data) {
                        $('#Source').val(data);
                        var original = $('#originalWikiContent');
                        original.hide();
                        $('#previewWikiContent').html(original.html()).show();
                        dlg.dialog('open');
                    });
                } else {
                    dlg.dialog('close');
                }
            });

            $('#Source').keyup(function (e) {
                if (timeout != null) {
                    clearTimeout(timeout);
                    timeout = null;
                }

                var self = $(this);
                timeout = setTimeout(function () {
                    $.post('@Url.RouteUrl("WikiAct", new { action = "GetWikiPreview", @Model._WikiContent.WikiTitle.Slug }) ',
                           { source: self.val() },
                           function (data) { $('#previewWikiContent').html(data); });
                }, 250);
            });

            $('#cancelEdit').click(function () {
                $('#editWikiForm').dialog('close');
            });
        });     
</script>
}
